from rest_framework import serializers
from users.models import User


class UserDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = [
            'id',
            'username',
            'password',
            'mobile',
            'email',
        ]
        extra_kwargs = {
            'password': {'write_only': True}
        }

    # 重写create方法，为了实现密码加密和添加is_staff=True
    def create(self, validated_data):
        # create_superuser里面是drf框架自带有的里面有set_password和把is_staff改称True(超级管理员)
        return self.Meta.model.objects.create_superuser(**validated_data)